package com.huike.web.controller.review;

import com.huike.business.domain.TbBusiness;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.review.domain.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * =========================================================新增数据============================================
     */

//    新增接口1
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult addUser(@PathVariable String name, @PathVariable int age, @PathVariable String sex) {
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        Boolean result = reviewService.addUser(review);
        if (result) {
            return AjaxResult.success("成功插入一条数据");
        } else {
            return AjaxResult.error("插入数据失败");
        }
    }

    //    新增接口2
/*    @PostMapping("/saveData")
    public AjaxResult saveData(@RequestBody Review review){
        Boolean result = reviewService.saveData(review);
        if (result){
            return AjaxResult.success("成功插入一条数据");
        }else {
            return AjaxResult.error("插入数据失败");
        }
    }*/
    @PostMapping("/saveData")
    public AjaxResult saveData(@RequestBody Review review){

        return toAjax(reviewService.insertTbReview(review));
    }

    /**
     * =========================================================删除数据=============================================
     */

    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable int id) {
        Boolean result = reviewService.remove(id);
        return AjaxResult.success("成功删除:1条数据");
    }

    /**
     * =========================================================修改数据=============================================
     */

/*    @PostMapping("update")
    public AjaxResult update(@RequestBody Review review) {
        Date nowDate = DateUtils.getNowDate();
        review.setUpdateTime(nowDate);
        Boolean result = reviewService.update(review);
        return AjaxResult.success("修改成功");
    }*/
    @PostMapping("update")
    public AjaxResult update(@RequestBody Review review)
    {
        return toAjax(ReviewService.updateTbReview(review));
    }

    /**
     * =========================================================查询数据=============================================
     */

    @GetMapping("getById")
    public AjaxResult getById(Integer id) {
        Review review = reviewService.getById(id);
        return AjaxResult.success(review);
    }

//    		/review/getDataByPage?pageNum=1&pageSize=10

//  分页查询

    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(Review review, HttpServletRequest req){
        startPage();
        List<Review> list = reviewService.getDataByPage(review);
        return getDataTable(list);
    }

}